package me.ezitku.base.dialog

import android.content.Context
import android.view.View
import android.widget.*
import androidx.fragment.app.Fragment
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.ug_project.views.findButton
import com.ug_project.views.findEditText
import com.ug_project.views.findListView
import com.ug_project.views.findTextView
import me.ezitku.base.extensions.inflate

/**
 * Created by yurqi on 2017/6/30.
 */


class _BottomSheetDialog : BottomSheetDialog {

    var rootView: View

    constructor(_context: Context, _rootView: View) : super(_context) {
        rootView = _rootView
    }

    constructor(_context: Context, _rootView: View, style: Int) : super(_context, style) {
        rootView = _rootView
    }




    //findViewById
    fun button(resId: Int): android.widget.Button = rootView.findButton(resId)

    fun imageView(resId: Int): android.widget.ImageView = rootView.findViewById(resId)

    fun linearLayout(resId: Int): android.widget.LinearLayout = rootView.findViewById(resId)

    fun relativeLayout(resId: Int): android.widget.RelativeLayout = rootView.findViewById(resId)

    fun listView(resId: Int): ListView = rootView.findListView(resId)

    fun textView(resId: Int): TextView = rootView.findTextView(resId)

    fun editText(resId: Int): EditText = rootView.findEditText(resId)

    fun switch(resId: Int): Switch = rootView.findViewById(resId)

    fun view(resId: Int): View = rootView.findViewById(resId)

    fun spinner(resId: Int): Spinner = rootView.findViewById(resId)

    fun checkBox(resId: Int): CheckBox = rootView.findViewById(resId)

    fun radioButton(resId: Int): RadioButton = rootView.findViewById(resId)

    fun radioGroup(resId: Int): RadioGroup = rootView.findViewById(resId)

    fun progressBar(resId: Int): ProgressBar = rootView.findViewById(resId)


}

fun Context.createBottomDialog(layoutId: Int,style: Int,
                               action: _BottomSheetDialog.() -> Unit): BottomSheetDialog {
    var rootView = layoutId.inflate
//    when (rootView) {
//        is ViewGroup -> {
//            rootView.font = FONT_NAME
//        }
//        is TextView -> {
//            rootView.font = FONT_NAME
//        }
//    }
    var dialog:_BottomSheetDialog
    if(style!=-1){
        dialog = _BottomSheetDialog(this, rootView,style)
    }else{
        dialog = _BottomSheetDialog(this, rootView)
    }
    action.invoke(dialog)

    dialog.setContentView(rootView)
    dialog.show()
    return dialog
}

fun Context.createBottomDialog(layoutId: Int, action: _BottomSheetDialog.() -> Unit): BottomSheetDialog {
    return createBottomDialog(layoutId,-1, action)
}

fun Fragment.createBottomDialog(layoutId: Int,
                                action: _BottomSheetDialog.() -> Unit): BottomSheetDialog {
    return activity!!.createBottomDialog(layoutId,-1, action)
}

fun Fragment.createBottomDialog(layoutId: Int, style: Int,
                                action: _BottomSheetDialog.() -> Unit): BottomSheetDialog {
    return activity!!.createBottomDialog(layoutId,style, action)
}
